<template>
  <Modal
    :options="getModalOptions"
    :on-positive-click="handleConfirm"
    :on-negative-click="handleClose"
    :on-close="handleClose"
  >
    <template #Context>
      <n-form
        ref="formRef"
        :model="userForm"
        :rules="userRules"
        :label-width="100"
        label-placement="left"
        require-mark-placement="left"
        :disabled="type === 'ignore'"
      >
        <template v-for="(item,index) in getFormOptions" :key="index">

          <n-form-item v-if="item.type === 'oss'" :label="item.label" :path="item.key">
            <fileOss :key-name="'imgList'" :limit="fileLen.length" :default-list="userForm[item.key]" @upload-status="statusfile" />
          </n-form-item>

          <n-form-item v-if="item.type == 'select'" :label="item.label" :path="item.key">
            <n-select v-model:value="userForm[item.key]" v-bind="item.props" />
          </n-form-item>

        </template>
      </n-form>
    </template>
  </Modal>

</template>

<script>
import { defineComponent, ref, reactive, computed, toRefs } from 'vue'
import { form, getquestionTypeList } from '../tools/form.js'
import Modal from '@/components/Modal/index.vue'
import fileOss from '@/components/UploadOss/fileUploadOss.vue'
import { inspectionIgnore } from '@/api/task/index'
import { ChangeQuestionType } from '@/api/question/index.js'

export default defineComponent({
  name: 'ConfirmModal',
  components: { Modal, fileOss },
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    type: {
      type: String,
      default: 'confirm'
    },
    data: {
      type: Object,
      default: () => null
    }
  },
  emits: {
    'update:visible': null,
    'reload': null
  },
  setup(props, { emit }) {
    const MODAL_TYPE = {
      'ignore': '忽略',
      'confirm': '确认'
    }
    getquestionTypeList()

    const { formItem, userForm, userRules } = form
    const formRef = ref()

    const data = reactive({
      userForm: {
        ...userForm,
        ...props.data
      },
      userRules: {
        ...userRules
      }
    })
    const getModalOptions = computed(() => {
      return {
        show: props.visible,
        title: MODAL_TYPE[props.type],
        negativeText: '取消',
        positiveText: '确认'
      }
    })

    const getFormOptions = computed(() => {
      return {
        ...formItem
      }
    })

    const fileStatus = ref('')
    const statusfile = async(status, list) => {
      fileStatus.value = status
      // if (status === 'success') {
      //   data.userForm = {
      //     ...data.userForm,
      //     ...list
      //   }
      // } else {
      //   data.userForm = {
      //     ...data.userForm,
      //     list
      //   }
      // }
    }

    function handleConfirm() {
      const params = props.data.id
      if (props.type === 'confirm') { // 确认
        ChangeQuestionType(`${params}/${data.userForm.questionCode}`).then(res => {
          if (res.code === 0) {
            emit('reload')
            handleClose()
          } else {
            $message.error(res.msg)
          }
        })
      } else if (props.type === 'ignore') { // 忽略
        inspectionIgnore(params).then(res => {
          if (res.code === 0) {
            emit('reload')
            handleClose()
          } else {
            $message.error(res.msg)
          }
        })
      } else {
        handleClose()
      }
    }

    /* 关闭弹窗 */
    const handleClose = () => {
      emit('update:visible', false)
    }

    const fileLen = props.data.fileImage.split(',')

    return {
      ...toRefs(data),
      formRef,
      getModalOptions,
      getFormOptions,
      handleConfirm,
      handleClose,
      statusfile,
      fileLen
    }
  }
})
</script>

<style lang="scss" scoped>
</style>
